﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace BST250301TestStand.Models.Helpers
{
    public static class AesEncryptor
    {
        // === 配置参数 ===

        // 密钥 (Key): 必须是 16, 24 或 32 字节长 (128, 192 或 256 位)
        // 实际应用中，请确保密钥是随机生成的且安全存储。
        private const string AesKey = "qindaolaibang18605323323"; // 32 字节 = 256 位

        // 初始化向量 (IV): 必须是 16 字节长
        // IV 用于确保相同的明文加密后得到不同的密文，应与 Key 一起安全存储或每次随机生成并附加到密文。
        private const string AesIV = "qdlbt18605323323"; // 16 字节 = 128 位

        // 默认编码
        private static readonly Encoding DefaultEncoding = Encoding.UTF8;

        /// <summary>
        /// 使用 AES 算法加密字符串
        /// </summary>
        /// <param name="plainText">待加密的明文字符串。</param>
        /// <returns>加密后的 Base64 字符串。</returns>
        public static string Encrypt(string plainText)
        {
            if (string.IsNullOrEmpty(plainText))
                throw new ArgumentNullException(nameof(plainText), "明文不能为空。");

            byte[] key = DefaultEncoding.GetBytes(AesKey);
            byte[] iv = DefaultEncoding.GetBytes(AesIV);

            using (Aes aesAlg = Aes.Create())
            {
                // 配置 AES 参数
                aesAlg.Key = key;
                aesAlg.IV = iv;
                aesAlg.Mode = CipherMode.CBC; // 密码块链接模式
                aesAlg.Padding = PaddingMode.PKCS7; // 填充模式

                // 创建加密器
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt, DefaultEncoding))
                        {
                            // 写入所有数据到流，并进行加密
                            swEncrypt.Write(plainText);
                        }
                        // 将加密后的字节数组转换为 Base64 字符串以便传输和存储
                        return Convert.ToBase64String(msEncrypt.ToArray());
                    }
                }
            }
        }

        /// <summary>
        /// 使用 AES 算法解密 Base64 字符串
        /// </summary>
        /// <param name="cipherText">待解密的 Base64 密文。</param>
        /// <returns>解密后的明文字符串。</returns>
        public static string Decrypt(string cipherText)
        {
            try
            {
            byte[] key = DefaultEncoding.GetBytes(AesKey);
            byte[] iv = DefaultEncoding.GetBytes(AesIV);
            byte[] cipherBytes = Convert.FromBase64String(cipherText);

            using (Aes aesAlg = Aes.Create())
            {
                // 配置 AES 参数
                aesAlg.Key = key;
                aesAlg.IV = iv;
                aesAlg.Mode = CipherMode.CBC;
                aesAlg.Padding = PaddingMode.PKCS7;

                // 创建解密器
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                using (MemoryStream msDecrypt = new MemoryStream(cipherBytes))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt, DefaultEncoding))
                        {
                            // 读取解密后的数据
                            return srDecrypt.ReadToEnd();
                        }
                    }
                }
            }


            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
    }
}